﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Xml;

namespace WebApplication1
{
    public partial class RSS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
			Response.ContentType = "text/xml";
			XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
			objX.WriteStartDocument();
			objX.WriteStartElement("rss");
			objX.WriteAttributeString("version","2.0");
			objX.WriteStartElement("channel");
			objX.WriteElementString("title", "Proeftuin info.");
			objX.WriteElementString("link","Home.aspx");
			objX.WriteElementString("description","Het laatste nieuws over de proeftuin.");
			objX.WriteElementString("ttl","5");
            SqlConnection objConnection = new SqlConnection();
            objConnection.ConnectionString = ConfigurationSettings.AppSettings["RSSConnectionString"];
            objConnection.Open();
			string sql = "SELECT TOP 10 Title, Body, Date FROM Table1 ORDER BY Date DESC";
			SqlCommand objCommand = new SqlCommand(sql, objConnection);
			SqlDataReader objReader = objCommand.ExecuteReader();
			while (objReader.Read())
			{
				objX.WriteStartElement("item");
				objX.WriteElementString("title",objReader.GetString(0));
				objX.WriteElementString("description",objReader.GetString(1));
               // objX.WriteElementString("link", "http://localhost:2358/home.aspx" + Convert.ToString(objReader.GetInt32(2)));
				objX.WriteElementString("pubDate", objReader.GetDateTime(2).ToString());
				objX.WriteEndElement();
			}
			objReader.Close();
			objConnection.Close();

			objX.WriteEndElement();
			objX.WriteEndElement();
			objX.WriteEndDocument();
			objX.Flush();
			objX.Close();
			Response.End();
        }
    }
}